package com.nari.ncps.screen.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nari.ncps.screen.entity.al.AlRealtimePollutant;
import com.nari.ncps.screen.mapper.al.AlRealtimePollutantMapper;
import com.nari.ncps.screen.service.al.AlRealtimePollutantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.vo.al.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 污染物排放浓度实时数据 服务实现类
 * </p>
 *
 * @author wanghuiqiang
 * @since 2025-11-06
 */
@Service
public class AlRealtimePollutantServiceImpl extends ServiceImpl<AlRealtimePollutantMapper, AlRealtimePollutant> implements AlRealtimePollutantService {

    @Autowired
    private AlRealtimePollutantMapper alRealtimePollutantMapper;

    @Override
    public AlRealtimePollutantEfficiencyVO getAlRealtimePollutantEfficiency(String orgCode) {

        LambdaQueryWrapper<AlRealtimePollutant> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(AlRealtimePollutant::getOrgCode, orgCode);
//        lqw.in(AlRealtimePollutant::getAssetCode, "1880-JH01ETL002XS001","1818-DJ01SCA001","1819-DJ03SCA001","1880-DJ01DJX001");
        lqw.orderByDesc(AlRealtimePollutant::getCreateTime);
        lqw.isNotNull(AlRealtimePollutant::getSeriesU);
        lqw.last("LIMIT 1");

        AlRealtimePollutantEfficiencyVO vo = new AlRealtimePollutantEfficiencyVO();
        List<AlRealtimePollutant> entityList = alRealtimePollutantMapper.selectList(lqw);
        for(AlRealtimePollutant entity:entityList){
            vo.setYear(entity.getYear());
            vo.setMonth(entity.getMonth());
            vo.setSeriesI(entity.getSeriesU());
            vo.setSeriesU(entity.getSeriesU());
            vo.setAssetCode(entity.getAssetCode());
        }

        return vo;
    }

    public List<AlRealtimePollutantConsumVO> getAlRealtimePollutantConsum(String orgCode) {

        LambdaQueryWrapper<AlRealtimePollutant> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(AlRealtimePollutant::getOrgCode, orgCode);
//        lqw.in(AlRealtimePollutant::getAssetCode, "1880-JH01ETL002XS001");
        lqw.isNotNull(AlRealtimePollutant::getRealTimePowerLoad);
        lqw.orderByDesc(AlRealtimePollutant::getCreateTime);
        lqw.last("LIMIT 24");

        List<AlRealtimePollutantConsumVO> voList = new ArrayList<>();
        AlRealtimePollutantConsumVO vo = null;
        List<AlRealtimePollutant> entityList = alRealtimePollutantMapper.selectList(lqw);
        int timeTable = 0;
//        int hour = LocalDateTime.now().getHour();
        for(AlRealtimePollutant entity:entityList){
            vo = new AlRealtimePollutantConsumVO();
            vo.setYear(entity.getYear());
            vo.setMonth(entity.getMonth());
            vo.setTime(String.valueOf(timeTable));
            vo.setRealTimePowerLoad(entity.getRealTimePowerLoad());
            vo.setAssetCode(entity.getAssetCode());
            timeTable++;

            voList.add(vo);
        }


        return voList;
    }

    @Override
    public AlRealtimePollutantSafetyVO getAlRealtimePollutantSafety(String orgCode) {

        LambdaQueryWrapper<AlRealtimePollutant> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(AlRealtimePollutant::getOrgCode, orgCode);
//        lqw.in(AlRealtimePollutant::getAssetCode, "1880-JH01ETL002XS001","1818-DJ01SCA001","1819-DJ03SCA001","1880-DJ01DJX001");
        lqw.orderByDesc(AlRealtimePollutant::getCreateTime);
        lqw.last("LIMIT 1");

        AlRealtimePollutantSafetyVO vo = new AlRealtimePollutantSafetyVO();
        List<AlRealtimePollutant> entityList = alRealtimePollutantMapper.selectList(lqw);
        for(AlRealtimePollutant entity:entityList){
            vo.setYear(entity.getYear());
            vo.setMonth(entity.getMonth());
            vo.setFluorideLevel(entity.getFluorideLevel());
            vo.setFluorideLevelHis(entity.getFluorideLevel());
            vo.setPmLevel(entity.getPmLevel());
            vo.setPmLevelHis(entity.getPmLevel());
            vo.setSo2Level(entity.getSo2Level());
            vo.setSo2LevelHis(entity.getSo2Level());

        }

        return vo;
    }



}
